package com.surekam.dma.web.dma;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.surekam.dma.entity.dma.Post;
import com.surekam.dma.entity.dma.Subject;
import com.surekam.dma.service.dma.PostManager;
import com.surekam.dma.service.dma.SubjectManager;
import com.surekam.dma.web.CrudActionSupport;

@Namespace("/demo")
//@Results( { @Result(name = PostAction.JUMP_POST, location = "posts.jsp", type = "dispatcher") })
public class PostAction extends CrudActionSupport<Post> {
	private static final long serialVersionUID = -4693178722448989462L;
	
	//public static final String JUMP_POST = "posts";
	
	private Long id;
	private Post entity;
	private Subject subject;
	private Page<Post> page = new Page<Post>(1);//每页12条记录
	private PostManager postManager;
	private SubjectManager subjectManager;

	public Post getModel() {
		return entity;
	}

	
	protected void prepareModel() throws Exception {
		if (id != null) {
			entity = postManager.getPost(id);
		} else {
			entity = new Post();
		}
	}

	public String list() throws Exception {
		subject = this.subjectManager.getSubject(id);
		List<PropertyFilter> filters = new ArrayList<PropertyFilter>();//PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
		PropertyFilter pf = new PropertyFilter("EQL_subject.id", id.toString());
		filters.add(pf);
		//设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("date");
			page.setOrder(Page.DESC);
		}
		page = postManager.searchPost(page, filters);
		return SUCCESS;
	}

	
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	
	public String save() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Autowired
	public void setPostManager(PostManager postManager) {
		this.postManager = postManager;
	}

	public Page<Post> getPage() {
		return page;
	}

	public Subject getSubject() {
		return subject;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	public Long getId() {
		return id;
	}

	@Autowired
	public void setSubjectManager(SubjectManager subjectManager) {
		this.subjectManager = subjectManager;
	}
}
